﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;

namespace 包覆门.MxExpression
{
    public partial class MxExpressionList : Form
    {
        private Dictionary<int, string> dicpb = new Dictionary<int, string>();
        private MxExpressionDAL dal = new MxExpressionDAL();

        public MxExpressionList()
        {
            InitializeComponent();
            this.dgMxExpression.AutoGenerateColumns = false;
        }
        //加载
        private void MxExpressionList_Load(object sender, EventArgs e)
        {
            dicpb.Add(0, "否");
            dicpb.Add(1, "是");
            BindingSource bs = new BindingSource();
            bs.DataSource = dicpb;
            this.ispb.DataSource = bs;
            this.ispb.DisplayMember = "value";
            this.ispb.ValueMember = "key";
            LoadData();
        }

        //加载数据
        public void LoadData() {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic["mxtype"] = this.tbname.Text.Trim();
            this.dgMxExpression.DataSource = dal.GetMxExpressionList(dic);
        }
        //关闭时执行清空操作
        private void MxExpressionList_FormClosing(object sender, FormClosingEventArgs e)
        {
            mainform.mxlist = null;
        }
        //关闭
        private void tsbClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        //查询
        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();

        }
        //新增
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            MxExpressionEditing me = new MxExpressionEditing();
            me.IsNew = true;
            me.IsCopy = false;
            if (me.ShowDialog() == DialogResult.OK) {
                LoadData();
            }
        }
        //编辑
        private void tsbEdit_Click(object sender, EventArgs e)
        {
             int count = dgMxExpression.SelectedRows.Count;
            if (count <=0) {
                MessageBox.Show("请选中要编辑的行");
                return;
            }
            MxExpressionEditing me = new MxExpressionEditing();
            me.IsNew = false;
            me.IsCopy = false;
            me.EditingId = this.dgMxExpression.SelectedRows[0].Cells["mid"].Value.ToString();
            if (me.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }
        //删除
        private void tsbDel_Click(object sender, EventArgs e)
        {
            int count = dgMxExpression.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要删除的行");
                return;
            }
            string uid = dgMxExpression.SelectedRows[0].Cells["mid"].Value.ToString();
            String info = "您确定要删除门型：" + dgMxExpression.SelectedRows[0].Cells["mxtype"].Value.ToString() + "的配置公式吗";
            if (DialogResult.OK == MessageBox.Show(info, "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
            {
                if (dal.DelById(uid)==1)
                {
                    MessageBox.Show("删除成功");
                    LoadData();
                }
                else
                {
                    MessageBox.Show("删除失败");
                    LoadData();
                }
            }
        }
        //复制
        private void tsbCopy_Click(object sender, EventArgs e)
        {
            int count = dgMxExpression.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要编辑的行");
                return;
            }
            MxExpressionEditing me = new MxExpressionEditing();
            me.IsNew = false;
            me.IsCopy = true;
            me.EditingId = this.dgMxExpression.SelectedRows[0].Cells["mid"].Value.ToString();
            if (me.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }
    }
}
